package com.sgb.goods.view.activity.vpm;

import android.text.TextUtils;
import android.view.WindowManager;
import android.widget.PopupWindow;

import androidx.lifecycle.Observer;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.sgb.goods.BR;
import com.sgb.goods.R;
import com.sgb.goods.databinding.GoodsActivityMemberPriceDetailsBinding;
import com.sgb.goods.entity.MemberPriceEntity;
import com.sgb.goods.utils.Constants;
import com.sgb.goods.utils.base.BaseActivity;
import com.sgb.goods.viewmodel.AddMemberGoodsModel;
import com.sgb.goods.widget.pop.ShowVipPriceDetailWindow;
import com.swgk.core.dialog.BasePopWindow;
import com.swgk.core.util.DateUtils;

import java.util.LinkedHashMap;
import java.util.List;

/**
 * 会员价详情
 * @author cyj
 */
@Route(path = Constants.ARouter.AR_MEMBER_PRICE_DETAILS)
public class MemberPriceDetailsActivity extends BaseActivity<GoodsActivityMemberPriceDetailsBinding, AddMemberGoodsModel> {

    private ShowVipPriceDetailWindow mPopWindow;
    private long itemMemberId;
    private long itemId;

    @Override
    protected int getLayoutResId() {
        return R.layout.goods_activity_member_price_details;
    }

    @Override
    protected void initViewModel() {
        mViewModel = createViewModel(AddMemberGoodsModel.class, BR.viewModel);
    }

    @Override
    protected void init() {
        mDataBinding.head.title.setText("会员价详情");
        itemMemberId = getIntent().getLongExtra("itemMemberId", 0);
        itemId = getIntent().getLongExtra("itemId", 0);
        mViewModel.getMemberPriceDetails(itemMemberId, itemId);
    }

    @Override
    protected void initObserve() {
        mViewModel.detail.observe(this, memberPriceEntity -> {
            if (memberPriceEntity.memberPriceSkuDetail != null)
                mViewModel.mAdapter.setNewData(memberPriceEntity.memberPriceSkuDetail);
            mDataBinding.tvUpdateTime.setText(DateUtils.timestampTo13Date3(memberPriceEntity.itemMember.createTime));
        });

        mViewModel.detailSku.observe(this, new Observer<List<MemberPriceEntity.MemberPriceSkuDetail>>() {
            @Override
            public void onChanged(List<MemberPriceEntity.MemberPriceSkuDetail> memberPriceSkuDetail) {
                LinkedHashMap<String, String> map = new LinkedHashMap<>();
                map.put("规格属性", memberPriceSkuDetail.get(0).skuDetails);
                if (memberPriceSkuDetail.get(0).parityPrice != null && (!TextUtils.isEmpty(memberPriceSkuDetail.get(0).promotionPrice)) && !memberPriceSkuDetail.get(0).promotionPrice.equals("0.00"))
                    map.put("销售价/促销价", "¥" + memberPriceSkuDetail.get(0).parityPrice + "/¥" + memberPriceSkuDetail.get(0).promotionPrice);
                if (memberPriceSkuDetail.get(0).parityPrice != null && (TextUtils.isEmpty(memberPriceSkuDetail.get(0).promotionPrice) || memberPriceSkuDetail.get(0).promotionPrice.equals("0.00")))
                    map.put("销售价", "¥" + memberPriceSkuDetail.get(0).parityPrice);
                if (memberPriceSkuDetail.get(0).parityPrice == null && memberPriceSkuDetail.get(0).promotionPrice != null)
                    map.put("促销价", "¥" + memberPriceSkuDetail.get(0).promotionPrice);
                for (int i = 0; i < memberPriceSkuDetail.size(); i++) {
                    switch (memberPriceSkuDetail.get(i).level) {
                        case 0:
                            map.put("普通会员", "¥" + memberPriceSkuDetail.get(i).price);
                            break;
                        case 1:
                            map.put("一星会员", "¥" + memberPriceSkuDetail.get(i).price);
                            break;
                        case 2:
                            map.put("二星会员", "¥" + memberPriceSkuDetail.get(i).price);
                            break;
                        case 3:
                            map.put("三星会员", "¥" + memberPriceSkuDetail.get(i).price);
                            break;
                        case 4:
                            map.put("四星会员", "¥" + memberPriceSkuDetail.get(i).price);
                            break;
                        case 5:
                            map.put("五星会员", "¥" + memberPriceSkuDetail.get(i).price);
                            break;
                    }
                }
                showPopWindow(map);
            }
        });
    }

    @Override
    protected void initListener() {
        mDataBinding.head.ivBack.setOnClickListener(v -> finish());
        mViewModel.mAdapter.setOnItemClickListener((adapter, view, position) -> {
            mViewModel.getSkuMemberPriceDetail(mViewModel.mAdapter.getData().get(position).itemMemberNo,
                    mViewModel.mAdapter.getData().get(position).skuId);
        });
        mDataBinding.imgLookDetail.setOnClickListener(v -> {
            mDataBinding.tvLookDetail.callOnClick();
        });
        mDataBinding.tvLookDetail.setOnClickListener(v -> {
            ARouter.getInstance().build(Constants.ARouter.AR_OPERATION_RECORD)
                    .withSerializable("MemberPriceEntity", mViewModel.detail.getValue()).navigation();
        });
    }

    private void showPopWindow(LinkedHashMap<String, String> map) {
        if (mPopWindow == null) {
            mPopWindow = new ShowVipPriceDetailWindow(this);
        }
        mPopWindow.setBgView(mDataBinding.grayLayout);
        mPopWindow.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
        mPopWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
        mPopWindow.setOnResultClick(new BasePopWindow.OnResultClick() {
            @Override
            public void result(Object key1, Object key2, Object key3) {
                mPopWindow.dismiss();
            }
        });
        mPopWindow.updateData(map, "会员价详情", "关闭");
        mPopWindow.showDownPopwindow(mDataBinding.grayLayout, true);
    }
}
